﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Linq;

namespace Sparkle.Hosting
{
    /// <summary>
    /// Interaction logic for UserControlLinks.xaml
    /// </summary>
    public partial class LinkPanel : UserControl, ILinkPanel
    {
        RoutedEventHandler onLinkClickEvent = null;

        public Type LinkType { get; set; }

        public LinkPanel()
        {
            InitializeComponent();
            onLinkClickEvent = new RoutedEventHandler(LinkButton_Click);

        }




        #region ILinkPanel Members

        public string Title { get { return this.txtTitle.Text; } set { this.txtTitle.Text = value; } }

        public UIElementCollection Links { get { return stkLinks.Children; } }

        public void PanelChanged(UserControl newObject)
        {
            if (newObject == null) return;

            if (OnControlChange != null)
            {
                OnControlChange(newObject, newObject.DataContext);
            }

            if (LinkType == null) return;

            var linkObjType = Activator.CreateInstance(LinkType);

            if (linkObjType is IControlAttribute)
            {
                ExtractLinks(LinkType, newObject, this.Links, onLinkClickEvent);
            }
            else if (linkObjType is IEntityAttribute)
            {
                ExtractLinks(LinkType, newObject.DataContext, this.Links, onLinkClickEvent);
            }
        }        

        public event LinkHandler OnLinkClick;

        public event LinkHandler OnControlChange;

        #endregion
        
        public virtual   void LinkButton_Click(object sender, RoutedEventArgs e)
        {
            if(OnLinkClick!=null)
            {
                OnLinkClick(sender, (sender is HyperlinkButton)  ? (sender as HyperlinkButton).Tag: null);
            }
        }

        #region Statics methods

        public static void ExtractLinks<T>(object linkContainer, UIElementCollection linkCollection, RoutedEventHandler onClick)
            where T:Attribute
        {

            ExtractLinks(typeof (T), linkContainer, linkCollection, onClick);
        }

        public static void ExtractLinks(Type type, object linkContainer, UIElementCollection linkCollection, RoutedEventHandler onClick)
        {
            if (linkContainer == null)
                return;
            if (linkCollection == null)
                return;

            linkCollection.Clear();

            var linksAttrib = linkContainer.GetType().GetCustomAttributes(type, true);

            foreach (var item in linksAttrib)
            {
                if (item is IDynamicLink)
                {
                    var dynamicLink = item as IDynamicLink;

                    var dymicLinks = dynamicLink.GetDynamicLinks(linkContainer);
                    if (dymicLinks != null)
                    {
                        foreach (var dynamiclink in dymicLinks)
                        {
                            var hLinkButton = new HyperlinkButton()
                                                              {Content = dynamiclink.ToString(), Tag = dynamiclink};
                            hLinkButton.Click += onClick;
                            linkCollection.Add(hLinkButton);
                        }
                    }
                }
                else if(item is IStaticLink)
                {
                    var hLinkButton = new HyperlinkButton() {Content = item.ToString(), Tag = item};
                    hLinkButton.Click += onClick;
                    linkCollection.Add(hLinkButton);
                }
            }
        }
        #endregion
    }
}
